import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'homes',
      component: () => import('@/views/homes.vue'),
      children:[
        {
          path:'/home',
          name:'home',
          component:()=>import('@/views/home/home.vue')
        },
        {
          path:'/classify',
          name:'classify',
          component:()=>import('@/views/home/classify.vue')
        },
        {
          path:'/speciale',
          name:'speciale',
          component:()=>import('@/views/home/speciale.vue')
        },
        {
          path:'/car',
          name:'car',
          component:()=>import('@/views/home/car.vue')
        },
        {
          path:'/my',
          name:'my',
          component:()=>import('@/views/home/my.vue')
        },
        {
          path:'',
          redirect:'/home'
        }

      ]
    },
    {
      path:'/details',
      name:'details',
      component:()=>import('@/components/details.vue')
    },
    {
      path:'/search',
      name:'search',
      component:()=>import('@/views/search/search.vue')
    },
    {
      path:'/order',
      name:'order',
      component:()=>import('@/components/order.vue')
    },
    {
      path:'/goods',
      name:'goods',
      component:()=>import('@/views/goods/list.vue')
    },
    {
      path:'/login',
      name:'login',
      component:()=>import('@/views/login/login.vue')
    },
    {
      path:'/cls',
      redirect:'/'
    }
  ]
})

// 路由守卫
router.beforeEach((to,from,next)=>{
  console.log(localStorage.getItem('token'));
  const isLogin = localStorage.getItem('token')!=null ? true : false
  console.log(isLogin);
  if(to.path == '/login'){
    next()
  }else{
    isLogin ? next() : next('/login')
  }
})

export default router
